#! /usr/bin/python


def inc_seq(seq):
	"""Is passed a string i.e 'GGG' and must increment it (in binary G/E's) to the next value"""

	# Find the first occurence of a G	
	firstg = seq.index('G');
	
	# Generate the new sequence
	newseq = '';
	for i in range(firstg):
		newseq += 'G';
	newseq += 'E' + seq[firstg+1:];

	return newseq;


def is_suitable(seq):
	"""Check is this a sequence worth examining"""
	
	# First look for a 'GG'
	if seq.find('GG') == -1:
		return False;

	# Now look at how many E's are on the right hand side of the
	# docking site.
	# if it is more than 1 then this sequence is worth doing
	# because it will be identical to another one
	dockind = seq.rfind('GG');

	# If the docking site has more than 1 E after it it's not worth
	# doing.
	if dockind < (len(seq)-3):
		return False;

	# Or alternatively, if the site is so far away that it sticks of the left
	# side.
	dockind = seq.find('GG');
	if dockind > 4:
		return False;

	return True;


def gen_all_comb_len(len,seqs):
	"""Generates all the G/E combinations of the specified length
		and appends them to seqs."""
	
	seq = '';
	for i in range(len):
		seq += 'G';

	while seq.find('G') != -1:
		# If sequence isn't EEE there we haven't reached the stop
		if is_suitable(seq):
			seqs.append(seq);

		seq = inc_seq(seq);


def gen_all_seq():
	seqs = [];

	# Attempts to list all the ligands we might need
	
	for len in range(3,8):
		# Generate all the combinations of this length
		gen_all_comb_len(len,seqs);

	return seqs;


def fetch_all_seqs():
	seqs = gen_all_seq();

	print seqs;
	print len(seqs), " sequences to fetch."




# Make this a stand-alone program
if __name__ == "__main__":
	fetch_all_seqs();